
#define _CRT_SECURE_NO_WARNINGS

using namespace std;
#include "printer_list.h"


PrinterList::PrinterList()
    :mSUCCEED(false)
{}

void PrinterList::OnTestProgramStart(const UnitTest& )
{
    ::std::fstream file;
    file.open(FileName(), ::std::ios_base::out|::std::ios_base::trunc );
    if(file.fail())
        SetBroken();
}


void PrinterList::OnTestStart(const TestInfo& test_info)
{
    mCaseName=test_info.test_case_name();
    mTestName=test_info.name();

    ::std::fstream file(FileName(), ::std::ios_base::out|::std::ios_base::app );
    if(file.fail())
        return SetBroken();

    file<<MessageLaunch()<<" "<<mCaseName<<"."<<mTestName<<".....";
    file.flush();
}

void PrinterList::OnTestPartResult(const TestPartResult& test_part_result)
{
    if(IsBroken())
        return;

    ::std::fstream file(FileName(), ::std::ios_base::out|::std::ios_base::app );
    if(file.fail())
        return SetBroken();

    if(test_part_result.failed()) 
        file<<" "<<MessageFailed()<<endl; 
    else
        file<<" "<<MessageSuccessd()<<endl,
        mSUCCEED=true;
}

void PrinterList::OnTestEnd(const TestInfo& test_info)
{
    if(IsBroken())
        return;

    ::std::fstream file(FileName(), ::std::ios_base::out|::std::ios_base::app );
    if(file.fail())
        return SetBroken();
   
    if(mSUCCEED==true)
        return mSUCCEED = false, void();

    if (test_info.result()->Passed()) 
        file<<" "<<MessagePassed()<<endl;
}

void PrinterList::OnTestProgramEnd(const UnitTest& unit_test)
{
    if(IsBroken())
        return;

    ::std::fstream file(FileName(), ::std::ios_base::out|::std::ios_base::app );
    if(file.fail())
        return SetBroken();

    size_t nRun        = unit_test.test_to_run_count();
    size_t nTotalCase  = unit_test.total_test_case_count();
    size_t nTotalTest  = unit_test.total_test_count();
    size_t nSuccesCase = unit_test.successful_test_case_count();
    size_t nSuccesTest = unit_test.successful_test_count();
    size_t nDisable    = unit_test.disabled_test_count();
    size_t nFailed     = nTotalTest-nSuccesTest-nDisable;
    size_t nCaseFailed = nTotalCase-nSuccesCase;

    file<<'\n'
          <<MessageTotalLaunchedtests()<< nRun        <<'\n'
          <<MessageTotalCases()        << nTotalCase  <<"\n\n"
          <<MessageTotalEndedTests()   << nTotalTest  <<'\n'
          <<MessageTotalPassedCases()  << nSuccesCase <<'\n'
          <<MessageTotalPassedTests()  << nSuccesTest <<'\n'
          <<MessageTotalFailedCases()  << nCaseFailed <<'\n'
          <<MessageTotalFailedTests()  << nFailed     <<'\n';

    if(nDisable>0) 
        file<<MessageTotalDisabledTests()<<"............... "<<nDisable<<"\n\n";

    file<<MessageTotalPucket()<< (unit_test.Passed()? MessagePucketSuccess(): MessagePucketFailed() )<<endl;

}
